package org.myabstract.graph.model;

import java.util.ArrayList;
import java.util.List;

public class Reference<E extends INodeType, T extends INodeType> {

	private Node<T> elem1;

	private Node<E> elem2;

	private List<IArc> arcList;

	public Reference(Node<E> e1, Node<T> e2) throws Exception {
		if (check(e1, e2)) {
			this.elem1 = e2;
			this.elem2 = e1;
			arcList = new ArrayList<IArc>();
		} else {
			// System.err.println("the elements in a reference must be different");
			throw new Exception("the elements in a reference must be different");
		}
	}

	// checks not to have references to the same type
	private boolean check(Node<E> elem1, Node<T> elem2) {
		if (elem1.equals(elem2))
			return false;
		return true;
	}

	public <T extends INodeType> boolean contains(Class<T> obj) {
		if (obj != null) {
			if (obj.isInstance(elem1.getType()))
				return true;
			else if (obj.isInstance(elem2.getType()))
				return true;
		}
		return false;
	}

	public boolean addArc(IArc arc) {
		if (arc != null) {
			this.arcList.add(arc);
			return true;
		}
		return false;
	}

	public List<IArc> getArcList() {
		return arcList;
	}

	public Node<INodeType> getNode(Node<INodeType> elem) {
		if (elem.equals(elem1))
			return (Node<INodeType>) elem2;
		else
			return (Node<INodeType>) elem1;
	}

	public List<Node<INodeType>> getNodes() {
		List rez = new ArrayList<Node<INodeType>>();

		rez.add(elem1);
		rez.add(elem2); 

		return rez;
	}

}
